import QtQuick 2.0
import Toou2D 1.0
import cn.conchit 1.0
import QtQuick.Controls 2.0

//图像列表
TRectangle{
    id: imageList
    //anchors.top: dividerLine1.bottom
    anchors.bottom: parent.bottom
    width: parent.width / 8;
    color: "#196E6C";
    radius: 4
    theme.enabled: false;

    signal dealWithImage(var index)

    function openDealWithStep(step) {
        var index = imageList_body.currentIndex + step
        if (index < 0 || index >= ImageModel.count()) {
            return
        }

        imageList_body.openNewImage(imageList_body.currentIndex + step)
    }

    function openImageFolder(currentFolder) {
        var res = JSON.parse(ImageModel.openDir(currentFolder))
        if(res.retVal === 1) {
            TToast.showSuccess(res.retStr, TTimePreset.ShortTime2s);
            imageList_body.currentIndex = 0
            imageList_body.lastCurrentIndex = 0
            imageList_body.itemAtIndex(0).color = "#196E6C"
            ImageLabelModel.loadImage(0);
            dealWithImage(0)
        } else {
            TToast.showError(res.retStr, TTimePreset.ShortTime2s);
        }
    }

    TLabel{
        id: imageList_head
        x:10 ;
        y:10 ;
        color: "#FFF"
        text: "图片列表" ;
        height: 20
        anchors.horizontalCenter: parent.horizontalCenter;
        theme.enabled: false;
    }

    TRectangle{
        anchors.top: imageList_head.bottom
        anchors.bottom: parent.bottom;
        width: parent.width
        color: "#002B36";
        theme.enabled: false;


        ListView {
               id: imageList_body
               anchors.fill: parent
               width: parent.width
               spacing: 2
               clip: true
               model: ImageModel
               delegate: iamgeDelegate
               currentIndex: -1
               focus: true
               property int lastCurrentIndex: -1



               function openNewImage(imgIndex) {
                   imageList_body.currentIndex = imgIndex
                   if(imageList_body.currentIndex === imageList_body.lastCurrentIndex) {
                       return
                   }

                   if (imageList_body.lastCurrentIndex != -1) {
                       //ImageLabelModel.save();
                       if(imageList_body.itemAtIndex(imageList_body.lastCurrentIndex)){
                           imageList_body.itemAtIndex(imageList_body.lastCurrentIndex).color = "#9DD6E5"
                       }
                   }
           //                        imageList_body.currentIndex = index
                   imageList_body.lastCurrentIndex = imgIndex
                   imageList_body.itemAtIndex(imgIndex).color = "#196E6C"
                   dealWithImage(imgIndex)
               }

        }

        Component {
            id: iamgeDelegate

            Rectangle {
                height: 20
                radius: 3
                color: "#9DD6E5"
                width:imageList.width



                MouseArea {
                    anchors.fill: parent
                    hoverEnabled: true
                    onPressed: {
                        imageList_body.openNewImage(index)
                    }
                    onEntered: {
                        if (imageList_body.currentIndex === index) return
                        color = "#196E6C"
                    }
                    onExited: {
                         if (imageList_body.currentIndex === index) return
                         color = "#9DD6E5"
                    }
                }

                Text {
                    anchors.centerIn: parent
                    text: index +": " + imageName
                }
            }

        }


    }

}
